//
//  FunctionMenu.m
//  mcrmSAAS
//
//  Created by wjxfhxy on 14-7-7.
//  Copyright (c) 2014年 GuangZhouXuanWu. All rights reserved.
//

#import "FunctionMenu.h"

#define     MenuItemHeight         80

#define     MenuItemMinWidth        80

#define     MenuItemImageWidth      50

#define     MenuItemImageHeight      MenuItemImageWidth

#define     MenuItemSeparatorWidth      0.5

#define     MenuItemLeftRightBoderMargin    5

#define     MenuItemTopBottomBoderMargin    5

@interface FunctionMenu ()<XUIButtonDelegate>
{
    __weak UIView *_parentView;
    
    UIView *_backGroundContainView;
    
    NSUInteger _count;
    
    NSMutableArray *_controls;
    
    CGFloat _menuItemMinWidth;
    
    CGFloat _moveDistance;
}
@end

@implementation FunctionMenu

-(id)init
{
    self = [super init];
    
    _controls = [NSMutableArray new];
    
    _menuItemMinWidth = MenuItemMinWidth;
    
    _moveDistance = 0;
    
    self.clipsToBounds = YES;
    
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:CGRectZero];
    
    _controls = [NSMutableArray new];
    
    self.backgroundColor = [UIColor clearColor];
    
    return self;
}

-(id)initWithParentView:(UIView*)parentView menuItemCountPerRow:(NSUInteger)count
{
    self  = [self init];
    
    _backGroundContainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    _backGroundContainView.backgroundColor = [UIColor colorWithR:241 G:241 B:241 A:1];
    [self addSubview:_backGroundContainView];
    
    _count = count;
    
    _parentView = parentView;
    
    CGFloat top = [XUINavigationController navigationController].navigationBar.bottom;
    self.frame = CGRectMake(0, top, _parentView.width, _parentView.height - top);
    
    _menuItemMinWidth = (self.width - count * MenuItemSeparatorWidth + MenuItemSeparatorWidth) / count;
    
    self.hidden = YES;
    
    return self;
}

-(void)addMenuItemWithText:(NSString*)text image:(UIImage*)image andTag:(NSString*)tag
{
    XUIButton *btn = [XUIButton buttonWithStyleType:XUIButtonStyleTypeTopHeadImage delegate:self];
    btn.backgroundColor = _backGroundContainView.backgroundColor;
    btn.szTag = tag;
    [btn SetTitle:text];
    [btn SetImage:image];
    [btn SetTitleColor:[UIColor darkGrayColor]];
    [btn SetFont:[UIFont systemFontOfSize:12]];
    btn.frame = CGRectMake(_controls.count % _count * (_menuItemMinWidth + MenuItemSeparatorWidth) , _controls.count/_count * (MenuItemHeight + MenuItemSeparatorWidth), _menuItemMinWidth, MenuItemHeight);
    
    [_controls addObject:btn];
    
    [_backGroundContainView addSubview:btn];
    
    /*if(_controls.count % _count > 0)
    {
        UIView *v = [[UIView alloc]initWithFrame:CGRectMake(btn.right, btn.top, MenuItemSeparatorWidth, MenuItemHeight)];
        v.backgroundColor = [UIColor colorWithR:178 G:178 B:178 A:1];
        [_backGroundContainView addSubview:v];
    }
    
    if(_controls.count % _count == 1)
    {
        UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, btn.bottom, self.width, MenuItemSeparatorWidth)];
        v.backgroundColor = [UIColor colorWithR:178 G:178 B:178 A:1];
        [_backGroundContainView addSubview:v];
    }*/
    
    _backGroundContainView.frame = CGRectMake(0, 0, self.width, btn.bottom + MenuItemSeparatorWidth);
}

-(void)removeAllItems
{
    _moveDistance = 0;
    [_controls removeAllObjects];
    [_backGroundContainView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}

- (CGRect)button:(XUIButton *)btn imageContentRect:(CGRect)rect
{
    CGSize size = [UIImage caleFitImageSize:[btn GetImage].size targetSize:CGSizeMake(MenuItemImageWidth, MenuItemImageHeight)];
    
    return CGRectMake((btn.width - size.width)/2, MenuItemTopBottomBoderMargin + (MenuItemImageHeight - size.height)/2, size.width, size.height);
}

- (CGRect)button:(XUIButton *)btn titleContentRect:(CGRect)rect
{
    return CGRectMake(0, MenuItemTopBottomBoderMargin + MenuItemImageHeight, btn.width, btn.height - MenuItemImageHeight - MenuItemTopBottomBoderMargin - MenuItemTopBottomBoderMargin );
}

- (void)clickedUpInsideOfButton:(XUIButton *)btn
{
    [_delegate functionMenu:self clickTag:btn.szTag];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touche = [touches anyObject];
    CGPoint p = [touche locationInView:self];
    if(CGRectContainsPoint(self.bounds, p))
    {
        [_delegate functionMenu:self touchEmpty:nil];
    }
}

-(void)show
{
    if(_controls.count <= 0)
        return;
    
    if(self.hidden == NO)
        return;
    
    self.hidden = NO;
    
    if(_moveDistance <= 0.0)
    {
        _moveDistance = ((XUIButton*)_controls[_controls.count-1]).bottom + MenuItemSeparatorWidth;
//        for(int i = 0;i < self.subviews.count;i ++)
//        {
//            CCustomButtonEx *btn = self.subviews[i];
//            btn.bottom -= _moveDistance;
//        }
        
        _backGroundContainView.bottom -= _moveDistance;
    }
    
    [_parentView addSubview:self];
    
    _parentView.userInteractionEnabled = NO;

    [UIView animateWithDuration:0.3 animations:^
    {
        self.backgroundColor = [UIColor colorWithR:0 G:0 B:0 A:0.7];
        
//        for(int i = 0;i < self.subviews.count;i ++)
//        {
//            CCustomButtonEx *btn = self.subviews[i];
//            btn.bottom += _moveDistance;
//        }
        
        _backGroundContainView.bottom += _moveDistance;
        
    } completion:^(BOOL finished)
    {
        _parentView.userInteractionEnabled = YES;
    }];
}

-(void)dismiss
{
    if(self.hidden == YES)
        return;
    
    _parentView.userInteractionEnabled = NO;
    
    [UIView animateWithDuration:0.3 animations:^
    {
        self.backgroundColor = [UIColor clearColor];
        
//         for(int i = 0;i < self.subviews.count;i ++)
//         {
//             CCustomButtonEx *btn = self.subviews[i];
//             btn.bottom -= _moveDistance;
//         }
        
        _backGroundContainView.bottom -= _moveDistance;
        
    } completion:^(BOOL finished)
    {
        _parentView.userInteractionEnabled = YES;
        
        [self removeFromSuperview];
        
        self.hidden = YES;
    }];
}


@end
